package org.grill_fish.fla.basics
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	import flash.media.ID3Info;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	import flash.utils.Timer;
        import flash.media.SoundLoaderContext;
	
	import org.grill_fish.fla.events.GF_GETID3_EVENT;
	import org.grill_fish.fla.events.GF_LOAD_PROGRESS;
	import org.grill_fish.fla.events.GF_MUSIC_COMPLETE;
	import org.grill_fish.fla.events.GF_TIMER_EVENT;
	import org.grill_fish.fla.events.GF_VOLUMN_MUTEEVENT;
	
	public class MyPlayer extends EventDispatcher
	{
		private var _filename: String = "http://music.tabex.org/downloads/far_away_from_home.mp3";
		private var _request: URLRequest = null;
		private var _sound : Sound = new Sound();
		private var _is_playing: Boolean = false;
		private var _break_point: int = 0;
		private var _sound_chanel : SoundChannel = new SoundChannel();
		private var _timmer:Timer = new Timer(10, 0);
		private var _last_vol:SoundTransform = null;
		public function MyPlayer()
		{
		}
		
		public function setFileName(name:String):void{
			this._filename = name;
		}

		public function changeVolumn(v:Number):void{
			if(v >= 0){
				this._last_vol =  this._sound_chanel.soundTransform;
				var s:SoundTransform = this._last_vol;
				s.volume = v;
				this._sound_chanel.soundTransform = s;
			}
			else{
				var data:Object = new Object();
				data.gfvalue = this._sound_chanel.soundTransform.volume;
				this.dispatchEvent(new GF_VOLUMN_MUTEEVENT(data, GF_VOLUMN_MUTEEVENT.eventname));
			}
		}
		public function muteVolumn(act:Boolean = true):void{
			this._last_vol = this._sound_chanel.soundTransform;
			var s:SoundTransform = this._last_vol;
			if(act){
				s.volume = 0;
			}
			else{
				s.volume = this._last_vol.volume;
			}
			this._sound_chanel.soundTransform = s;
			var data:Object = new Object();
			data.gfvalue = s.volume;
			this.dispatchEvent(new GF_VOLUMN_MUTEEVENT(data, GF_VOLUMN_MUTEEVENT.eventname));
		}
		
		
		public function play(pos:int = 0):void{
 			if(pos != 0){
				this._sound_chanel = this._sound.play(pos);
				return;
			}
			try{
				this._sound_chanel.stop();
				// this._sound.removeEventListener(Event.ID3, this.sendID3);
				this._sound.removeEventListener(ProgressEvent.PROGRESS, this.loadProgress);
			}catch(e:Error){
			}
			if(this._request == null){
				this._request = new URLRequest(this._filename);
			}
			else{
				this._request.url = this._filename;
			}
                        // 预载20s
                        var slc:SoundLoaderContext = new SoundLoaderContext(20, true);
			this._sound = new Sound(this._request, slc);
			this._is_playing = true;
			// this._sound.addEventListener(Event.ID3, this.sendID3);
			this._sound.addEventListener(ProgressEvent.PROGRESS, this.loadProgress);
			this._sound_chanel.addEventListener(Event.SOUND_COMPLETE, this.play_complte);
			this._sound_chanel = this._sound.play(pos);

		}
		
		private function play_complte(obj:Object):void{
			this.dispatchEvent(new GF_MUSIC_COMPLETE(GF_MUSIC_COMPLETE.eventname));
		}
		
		private function loadProgress(obj:Object):void{
			var data:Object = new Object;
			data.bytes_all = obj.bytesTotal;
			data.bytes_load = obj.bytesLoaded;
			this.dispatchEvent(new GF_LOAD_PROGRESS(data, GF_LOAD_PROGRESS.eventname));
		}
		
		private function sendID3(obj:Object):void{
			var data:Object = new Object();
			var id3:ID3Info = this._sound.id3;
			data.id3 = id3;
			data.bytes_all = obj.currentTarget.bytesTotal;
			data.bytes_load = obj.currentTarget.bytesLoaded;
			this.dispatchEvent(new GF_GETID3_EVENT(data, GF_GETID3_EVENT.eventname));
		}
		
		public function pause():int{
			if(this._is_playing == false){
				return 0;
			}
			else{
				this._break_point = this._sound_chanel.position;
				this._sound_chanel.stop();
				return this._break_point;
			}
		}

	}
}
